<template>
    <div>
        <!--:model="shop" 数据双向绑定-->
        <!--ref="shopForm" id="shopForm",给form去一个名字-->
        <!--:rules="formRules" 校验规则-->
        <el-form :model="shop" ref="shopForm" :rules="formRules" label-position="left" label-width="100px" class="demo-ruleForm login-container">
            <h3 class="title">店铺入驻</h3>
            <el-form-item prop="name"label="名称">
                <el-input type="text" v-model="shop.name" auto-complete="off" placeholder="请输入店铺名称！"></el-input>
            </el-form-item>
            <el-form-item prop="tel" label="座机">
                <el-input type="text" v-model="shop.tel" auto-complete="off" placeholder="请输入座机！"></el-input>
            </el-form-item>
            <el-form-item label="店铺地址" prop="address">
                <!--</baidu-map>-->
                <!-- 百度地图 -->
                <baidu-map class="map" id="container" style="display: none" ></baidu-map>
                <baidu-map class="map" id="" :center="map.center" :zoom="map.zoom" @ready="handler">

                    <!--缩放-->
                    <bm-navigation anchor="BMAP_ANCHOR_TOP_LEFT"></bm-navigation>
                    <!--定位-->
                    <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :showAddressBar="true" :autoLocation="true"></bm-geolocation>
                    <!--点-->
                    <bm-marker :position="map.center" :dragging="map.dragging" animation="BMAP_ANIMATION_DROP">
                        <!--提示信息-->
                        <bm-info-window :show="map.show">{{map.message}}</bm-info-window>
                    </bm-marker>
                </baidu-map>

                <el-input v-model="shop.address" id="suggestId" auto-complete="off" placeholder="请输入地址！" style="width: 350px"
                          type="text"></el-input>
                <!--        <el-button size="small" type="primary" @click="selectAdrress">选择</el-button>-->
            </el-form-item>
            <el-form-item prop="logo" label="店铺Logo">
                <el-upload
                        class="upload-demo"
                        action="http://localhost:8888/fastDfs"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :on-success="handleSuccess"
                        :file-list="fileList"
                        list-type="picture">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>
            </el-form-item>
            <h3>管理员信息设置</h3>
            <el-form-item prop="username" label="账号">
                <el-input type="text" v-model="shop.username" auto-complete="off" placeholder="请输入账号！"></el-input>
            </el-form-item>
            <el-form-item prop="tel" label="手机号码">
                <el-input type="text" v-model="shop.phone" auto-complete="off" placeholder="请输入电话！"></el-input>
            </el-form-item>
            <el-form-item prop="email" label="电子邮件">
                <el-input type="text" v-model="shop.email" auto-complete="off" placeholder="请输入邮件！"></el-input>
            </el-form-item>
            <el-form-item prop="password" label="密码">
                <el-input type="password" v-model="shop.password" auto-complete="off" placeholder="请输入密码！"></el-input>
            </el-form-item>
            <el-form-item prop="comfirmPassword" label="确认密码">
                <el-input type="password" v-model="shop.comfirmPassword" auto-complete="off" placeholder="请输入确认密码！"></el-input>
            </el-form-item>
            <el-form-item style="width:100%;">
                <el-button type="primary" style="width:100%;" @click.native.prevent="settledIn" >入驻</el-button>
            </el-form-item>
        </el-form>
        <!--<el-dialog-->
                <!--title="选择地址"-->
                <!--:visible.sync="dialogVisable"-->
                <!--width="30%">-->
            <!--<baidu-map :center="{lng: 116.403765, lat: 39.914850}" :zoom="11">-->
                <!--<bm-view class="map"></bm-view>-->
                <!--<bm-control :offset="{width: '10px', height: '10px'}">-->
                    <!--&lt;!&ndash;:sugStyle="{zIndex: 2100} 让搜索提示上浮&ndash;&gt;-->
                    <!--<bm-auto-complete v-model="keyword" :sugStyle="{zIndex: 2100}">-->
                        <!--<div style="margin-bottom:10px">-->
                            <!--<input id="searchInput" type="text" placeholder="请输入关键字" class="searchinput"/>-->
                            <!--<el-button type="success" @click="selectAdrressConfirm">确定</el-button>-->
                        <!--</div>-->
                    <!--</bm-auto-complete>-->
                <!--</bm-control>-->
                <!--<bm-local-search :keyword="keyword" :auto-viewport="true" ></bm-local-search>-->
            <!--</baidu-map>-->
            <!--<span slot="footer" class="dialog-footer">-->
        <!--<el-button @click="dialogVisable=false">取 消</el-button>-->
        <!--<el-button type="primary" @click="dialogVisable=false">确 定</el-button>-->
      <!--</span>-->
        <!--</el-dialog>-->
    </div>
</template>


<script>

    import { shopSettlement, shopLogoDelete} from '../../src/api/api';


    export default {
        data() {
            var validatePass2 = (rule, value, callback) => {
                console.log(value); //确认密码
                if (value === '') {
                    callback(new Error('请再次输入密码'))
                } else if (value !== this.shop.password) {
                    callback(new Error('两次输入密码不一致!'))
                } else {
                    callback()
                }
            }
            return {
                map: {
                    center: {
                        lng:116.40387397,
                        lat: 39.91488908
                    },
                    zoom: 15,
                    show: true,
                    message:'',
                    dragging: true
                },
                keyword:'',
                dialogVisable:false,
                fileList: [],
                //shop:shop 为了做数据表单校验不要嵌套对象
                shop: {
                    name: '',
                    tel: '',
                    address: '',
                    logo:'',
                    username:'',
                    phone:'',
                    email:'',
                    password:'',
                    comfirmPassword:''
                },
                formRules: {
                    name: [
                        { required: true, message: '请输入店铺名称!', trigger: 'blur' }
                    ],
                    tel: [
                        { required: true, message: '请输入店铺座机!', trigger: 'blur' }
                    ],
                    address: [
                        { required: true, message: '请输入店铺地址!', trigger: 'blur' }
                    ],
                    // logo: [
                    //     { required: true, message: '请输入店铺logo!', trigger: 'blur' }
                    // ],
                    username: [
                        { required: true, message: '请输入账号!', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '请输入手机号!', trigger: 'blur' }
                    ],
                    // email: [
                    //     { type: 'email',required: true, message: '请输入邮箱!', trigger: 'blur' }
                    // ],
                    password: [
                        { required: true, message: '请输入密码!', trigger: 'blur' }
                    ],
                    comfirmPassword: [
                        {required: true,validator: validatePass2, trigger: 'blur' } //自定义校验规则
                    ]
                }
            };
        },
        methods: {

            handler({BMap, map}) {
                this.map.message = "点击红标确认地址，获取在搜索框中搜索"
                // 将vue对象获取出来方便在方法体中使用
                let me = this;
                console.log(BMap, map)
                // 创建map 和 input框绑定
                var ac = new BMap.Autocomplete({
                    "input": "suggestId",
                    "location": map
                });
                // 地图搜索功能
                // 鼠标放在下拉列表上事件
                ac.addEventListener("highlight", function (e) {
                    // debugger
                    var str = "";
                    var _value = e.fromitem.value;
                    var value = "";
                    // debugger
                    if (e.fromitem.index > -1) {
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }

                    // str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
                    // str = value;

                    value = "";
                    if (e.toitem.index > -1) {
                        _value = e.toitem.value;
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }
                    // str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                    str += value;
                    // me.shop.address = str;
                })
                // 鼠标点击下拉事件 - 点击下拉框中的某一个值之后才确定将值赋给input框
                ac.addEventListener("confirm", function (e) {
                    var _value = e.item.value;

                    var myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    // me.shop.address ="confirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
                    me.shop.address = myValue;
                    // debugger
                    me.setPlace(map, myValue);
                })
                // 鼠标缩放 - 开启通过鼠标缩放地图
                map.enableScrollWheelZoom(true)
                // 鼠标滚轮事件 - 放大或缩小地图之后改变中心位置不会缩放尺寸发生改变
                map.addEventListener("zoomend", function (e) {
                    // 获取改变后的zoom大小
                    var ZoomNum = map.getZoom();
                    console.log(ZoomNum)
                    me.map.zoom = ZoomNum;
                    // debugger
                })
                // 点击事件获取经纬度
                map.addEventListener('click', function (e) {

                    // console.log(this.center.lat)
                    me.map.center.lng = e.point.lng
                    me.map.center.lat = e.point.lat
                    // 通过经纬度逆编码获取地理信息

                    // 传入密钥获取地图回调。 通过经纬度获取地址
                    me.getAddress(e.point.lng, e.point.lat, me)
                    console.log(BMap)
                    // console.log(BMPGL(me.ak))
                    // debugger

                    console.log(e.point.lng, e.point.lat)
                })
            },
            // 点击搜索之后重置地图坐标
            setPlace(map, myValue) {
                this.map.message = myValue;
                this.map.show = false;
                var _this = this;
                // map.clearOverlays();    //清除地图上所有覆盖物
                // debugger
                function myFun(){
                    // pp 对象中包含经纬度
                    var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                    // debugger
                    // 点击之后重新设置中心坐标
                    _this.map.center.lng = pp.lng;
                    _this.map.center.lat = pp.lat;
                    _this.map.zoom = 15
                    // map.centerAndZoom(pp, 18);
                    // map.addOverlay(new BMap.Marker(pp));    //添加标注
                }
                var local = new BMap.LocalSearch(map, { //智能搜索
                    onSearchComplete: myFun
                });
                local.search(myValue);
            },
            // 通过经纬度获取地址
            getAddress(lng, lat, me) {
                var map = new BMap.Map("container");
                // debugger
                map.centerAndZoom( new BMap.Point(lng, lat), me.map.zoom);
                // 创建地理编码实例
                var myGeo =  new BMap.Geocoder();
                // 根据坐标得到地址描述
                myGeo.getLocation(new BMap.Point(lng, lat), function(result){
                    if (result){
                        // alert(result.address);
                        me.shop.address = result.address;
                        me.map.message = result.address;
                    }
                });
            },

            selectAdrressConfirm() {
                //获取值搜索框值,设置给地址
                var searchInputV = document.getElementById("searchInput").value;
                this.shop.address = searchInputV;
                //关闭对话框
                this.dialogVisable = false;
            },
            selectAdrress() {
                this.dialogVisable = true;
            },

            selectAdrressConfirm(){
                //获取值搜索框值,设置给地址
                var searchInputV=document.getElementById("searchInput").value;
                this.shop.address = searchInputV;
                //关闭对话框
                this.dialogVisable = false;
            },
            selectAdrress(){
                this.dialogVisable = true;
            },
            handleSuccess(response, file, fileList){
                console.log("===========")
                console.log(response);
                console.log(file);
                console.log(fileList);
                this.shop.logo = response.data;
                // shopLogoUpload()
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                let path = file.response.data;
                shopLogoDelete(path)
                    .then(result=>{
                        result = result.data;
                        if(result.success){
                            this.$message({
                                message: "删除成功!",
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: result.message,
                                type: 'error'
                            });
                        }
                    })
            },
            handlePreview(file) { //预览
                console.log(file);
            },
            settledIn(){
                this.$refs.shopForm.validate((valid) => {
                    //校验表单成功后才做一下操作
                    if (valid) {
                        this.$confirm('确认入驻吗？', '提示', {}).then(() => {
                            //拷贝后面对象的值到新对象,防止后面代码改动引起模型变化
                            let para = Object.assign({}, this.shop); //shop 本身这个参数里面就有店铺和管理员信息
                            // 为了后台好接收，封装一个对象放到里面
                            let admin = {
                                username: para.username,
                                phone: para.phone,
                                email: para.email,
                                password:para.password,
                                comfirmPassword:para.comfirmPassword
                            }
                            para.admin = admin;
                            //判断是否有id有就是修改,否则就是添加
                            shopSettlement(para).then((res) => {
                                if(res.data.success){
                                    this.$message({
                                        message: '操作成功!',
                                        type: 'success'
                                    });
                                    //重置表单
                                    this.$refs['shopForm'].resetFields();
                                    //跳转登录页面
                                    this.$router.push({ path: '/login' });
                                }
                                else{
                                    this.$message({
                                        message: res.data.message,
                                        type: 'error'
                                    });
                                }

                            });
                        });
                    }
                })
            },
            // 创建地图实例

        }
    }


</script>
<style lang="scss" scoped>
    .login-container {
        -webkit-border-radius: 5px;
        border-radius: 5px;
        -moz-border-radius: 5px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 500px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
        .title {
            margin: 0px auto 40px auto;
            text-align: center;
            color: #505458;
        }
        .remember {
            margin: 0px 0px 35px 0px;
        }
    }

    .map {
        width: 100%;
        height: 300px;
    }
    .searchinput{
        width: 300px;
        box-sizing: border-box;
        padding: 9px;
        border: 1px solid #dddee1;
        line-height: 20px;
        font-size: 16px;
        height: 38px;
        color: #333;
        position: relative;
        border-radius: 4px;
    }
</style>